import { _ as __unplugin_components_4 } from "./SongList.js";
import { d as defineComponent, as as useDataStore, r as ref, L as shallowRef, f as computed, bI as renderIcon, h as watchDebounced, cW as fuzzySearch, T as onActivated, o as onMounted, bM as resolveDirective, H as createElementBlock, j as openBlock, J as createBaseVNode, n as createVNode, m as withCtx, a7 as createTextVNode, ai as __unplugin_components_0, a5 as __unplugin_components_1, ce as __unplugin_components_2, k as unref, d8 as __unplugin_components_3, a8 as toDisplayString, ar as __unplugin_components_3$1, i as createBlock, af as createCommentVNode, bN as withDirectives, a6 as Button, c5 as __unplugin_components_5, aq as __unplugin_components_6, ao as isRef, b4 as __unplugin_components_5$1, ae as Transition, aR as formatSongsList, K as _export_sfc } from "./stores.js";
import { u as useRouter, d as openBatchList, p as player, f as userCloud } from "./index.js";
const _hoisted_1 = { class: "cloud" };
const _hoisted_2 = { class: "title" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "Cloud",
  setup(__props) {
    const router = useRouter();
    const dataStore = useDataStore();
    const isActivated = ref(false);
    const loading = ref(false);
    const cloudCount = ref(0);
    const cloudData = shallowRef(dataStore.cloudPlayList);
    const cloudSize = ref({ size: 0, maxSize: 0 });
    const searchValue = ref("");
    const searchData = ref([]);
    const listDataShow = computed(() => {
      if (searchValue.value && searchData.value.length) return searchData.value;
      return cloudData.value;
    });
    const isCloudPage = computed(() => router.currentRoute.value.name === "cloud");
    const moreOptions = computed(() => [
      {
        label: "批量操作",
        key: "batch",
        props: {
          onClick: () => openBatchList(cloudData.value, false)
        },
        icon: renderIcon("Batch")
      }
    ]);
    const getAllCloudMusic = async () => {
      loading.value = true;
      let offset = 0;
      const limit = 500;
      const listData = [];
      do {
        const result = await userCloud(limit, offset);
        const songData = formatSongsList(result.data);
        cloudCount.value = result.count;
        cloudSize.value = {
          size: Number((result.size / Math.pow(1024, 3)).toFixed(2)),
          maxSize: Number((result.maxSize / Math.pow(1024, 3)).toFixed(0))
        };
        listData.push(...songData);
        cloudData.value = listData;
        offset += limit;
      } while (offset < cloudCount.value && isCloudPage.value);
      dataStore.setCloudPlayList(cloudData.value);
      loading.value = false;
    };
    watchDebounced(
      () => [searchValue.value, cloudData.value],
      () => {
        const search = searchValue.value.trim();
        if (!search || search === "" || !cloudData.value.length) return;
        const result = fuzzySearch(search, cloudData.value);
        searchData.value = result;
      },
      { debounce: 300, maxWait: 1e3 }
    );
    onActivated(() => {
      if (!isActivated.value) {
        isActivated.value = true;
      } else {
        getAllCloudMusic();
      }
    });
    onMounted(getAllCloudMusic);
    return (_ctx, _cache) => {
      const _component_n_text = __unplugin_components_0;
      const _component_SvgIcon = __unplugin_components_1;
      const _component_n_number_animation = __unplugin_components_2;
      const _component_n_progress = __unplugin_components_3;
      const _component_n_flex = __unplugin_components_3$1;
      const _component_n_button = Button;
      const _component_n_dropdown = __unplugin_components_5;
      const _component_n_input = __unplugin_components_6;
      const _component_SongList = __unplugin_components_4;
      const _component_n_empty = __unplugin_components_5$1;
      const _directive_debounce = resolveDirective("debounce");
      return openBlock(), createElementBlock("div", _hoisted_1, [
        createBaseVNode("div", _hoisted_2, [
          createVNode(_component_n_text, { class: "keyword" }, {
            default: withCtx(() => _cache[1] || (_cache[1] = [
              createTextVNode("我的云盘")
            ])),
            _: 1
          }),
          createVNode(_component_n_flex, { class: "status" }, {
            default: withCtx(() => [
              createVNode(_component_n_text, { class: "item" }, {
                default: withCtx(() => [
                  createVNode(_component_SvgIcon, {
                    name: "Music",
                    depth: 3
                  }),
                  createVNode(_component_n_number_animation, {
                    from: 0,
                    to: unref(cloudCount) || unref(cloudData)?.length || 0
                  }, null, 8, ["to"]),
                  _cache[2] || (_cache[2] = createTextVNode(" 首 "))
                ]),
                _: 1
              }),
              createVNode(_component_n_text, { class: "item" }, {
                default: withCtx(() => [
                  createVNode(_component_SvgIcon, {
                    name: "Storage",
                    depth: 3
                  }),
                  createVNode(_component_n_progress, {
                    percentage: 100 / (unref(cloudSize).maxSize / unref(cloudSize).size),
                    class: "status",
                    type: "line"
                  }, {
                    default: withCtx(() => [
                      createVNode(_component_n_text, {
                        class: "space",
                        depth: "3"
                      }, {
                        default: withCtx(() => [
                          createTextVNode(toDisplayString(unref(cloudSize).size ?? 0) + "GB / " + toDisplayString(unref(cloudSize).maxSize ?? 0) + "GB ", 1)
                        ]),
                        _: 1
                      })
                    ]),
                    _: 1
                  }, 8, ["percentage"])
                ]),
                _: 1
              })
            ]),
            _: 1
          })
        ]),
        createVNode(_component_n_flex, {
          class: "menu",
          justify: "space-between"
        }, {
          default: withCtx(() => [
            createVNode(_component_n_flex, {
              class: "left",
              align: "flex-end"
            }, {
              default: withCtx(() => [
                withDirectives((openBlock(), createBlock(_component_n_button, {
                  focusable: false,
                  disabled: unref(loading) || !unref(cloudData)?.length,
                  loading: unref(loading),
                  type: "primary",
                  strong: "",
                  secondary: "",
                  round: ""
                }, {
                  icon: withCtx(() => [
                    createVNode(_component_SvgIcon, { name: "Play" })
                  ]),
                  default: withCtx(() => [
                    createTextVNode(" " + toDisplayString(unref(loading) ? `
              正在更新... (${unref(cloudData).length === unref(cloudCount) ? 0 : unref(cloudData).length}/${unref(cloudCount)})` : "播放"), 1)
                  ]),
                  _: 1
                }, 8, ["disabled", "loading"])), [
                  [_directive_debounce, () => unref(player).updatePlayList(unref(cloudData))]
                ]),
                createVNode(_component_n_button, {
                  focusable: false,
                  class: "more",
                  strong: "",
                  secondary: "",
                  circle: "",
                  onClick: getAllCloudMusic
                }, {
                  icon: withCtx(() => [
                    createVNode(_component_SvgIcon, { name: "Refresh" })
                  ]),
                  _: 1
                }),
                createVNode(_component_n_dropdown, {
                  options: unref(moreOptions),
                  trigger: "click",
                  placement: "bottom-start"
                }, {
                  default: withCtx(() => [
                    createVNode(_component_n_button, {
                      focusable: false,
                      class: "more",
                      circle: "",
                      strong: "",
                      secondary: ""
                    }, {
                      icon: withCtx(() => [
                        createVNode(_component_SvgIcon, { name: "List" })
                      ]),
                      _: 1
                    })
                  ]),
                  _: 1
                }, 8, ["options"])
              ]),
              _: 1
            }),
            unref(cloudData)?.length ? (openBlock(), createBlock(_component_n_input, {
              key: 0,
              value: unref(searchValue),
              "onUpdate:value": _cache[0] || (_cache[0] = ($event) => isRef(searchValue) ? searchValue.value = $event : null),
              "input-props": { autocomplete: "off" },
              class: "search",
              placeholder: "模糊搜索",
              clearable: "",
              round: ""
            }, {
              prefix: withCtx(() => [
                createVNode(_component_SvgIcon, { name: "Search" })
              ]),
              _: 1
            }, 8, ["value"])) : createCommentVNode("", true)
          ]),
          _: 1
        }),
        createVNode(Transition, {
          name: "fade",
          mode: "out-in"
        }, {
          default: withCtx(() => [
            !unref(searchValue) || unref(searchData)?.length ? (openBlock(), createBlock(_component_SongList, {
              key: 0,
              data: unref(listDataShow),
              loading: unref(loading)
            }, null, 8, ["data", "loading"])) : (openBlock(), createBlock(_component_n_empty, {
              key: 1,
              description: `搜不到关于 ${unref(searchValue)} 的任何歌曲呀`,
              style: { "margin-top": "60px" },
              size: "large"
            }, {
              icon: withCtx(() => [
                createVNode(_component_SvgIcon, { name: "SearchOff" })
              ]),
              _: 1
            }, 8, ["description"]))
          ]),
          _: 1
        })
      ]);
    };
  }
});
const Cloud = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-82464c31"]]);
export {
  Cloud as default
};
